/**
 * @Title: DatbAddLoginSyncLogic.java
 * @Package: com.jason.fingerprint.logic
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月4日23:35:19
 * @Version: V1.0
 */
package com.jason.fingerprint.logic;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.jason.fingerprint.AppContext;
import com.jason.fingerprint.beans.AddLoginInfoReq;
import com.jason.fingerprint.common.UIHelper;
import com.jason.fingerprint.configs.HttpConfig;
import com.jason.fingerprint.utils.CodecUtils;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;
import okhttp3.Headers;

/**
 * @ClassName: DatbAddLoginSyncLogic
 * @Description: 添加登录日志
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月4日23:34:57
 */
public class DatbAddLoginSyncLogic extends Logic {

	private static final String TAG = "DatbAddLoginSyncLogic";

	private Handler mHandler;
	private boolean mIsCallBack = false;// 是否有回调，此需要Handle,默认不回调
	private Message mMessage;
	private AddLoginInfoReq mAddLoginInfoReq;

	public DatbAddLoginSyncLogic() {

	}

	public DatbAddLoginSyncLogic(AppContext appContext, Handler handler) {
		super(appContext);
		this.mHandler = handler;
		this.mMessage = new Message();
	}

	public DatbAddLoginSyncLogic(AppContext appContext, Handler handler,
			boolean isCallBack) {
		super(appContext);
		this.mHandler = handler;
		this.mMessage = new Message();
		this.mIsCallBack = isCallBack;
	}
	
	public DatbAddLoginSyncLogic(AppContext appContext, Handler handler,
			boolean isCallBack,AddLoginInfoReq loginInfoReq) {
		super(appContext);
		this.mHandler = handler;
		this.mMessage = new Message();
		this.mIsCallBack = isCallBack;
		this.mAddLoginInfoReq = loginInfoReq;
	}

	@Override
	public void execute() {
		datbAddLoginInfoSync();
	}

	@Override
	public void send() {
		if (mIsCallBack) {
			mHandler.sendMessage(mMessage);
		}
	}

	// 档案同步
	private void datbAddLoginInfoSync() {
		mParams = new RequestParams();
		mParams.addFormDataPart("type", String.valueOf(mAddLoginInfoReq.getType()));
		mParams.addFormDataPart("loginName", mAddLoginInfoReq.getLoginName());
		mParams.addFormDataPart("userName",CodecUtils.base64Encode(mAddLoginInfoReq.getUserName()) );
		//TODO mAddLoginInfoReq.getOrgan()不能为空
		mParams.addFormDataPart("organ", CodecUtils.base64Encode(mAddLoginInfoReq.getOrgan()));
		mParams.addFormDataPart("organId", mAddLoginInfoReq.getOrganId());
		String url = HttpConfig.POST_URL_DATB_ADDLOGININFO;
		Log.i(TAG, "----->API:" + url);
		Log.i(TAG, "----->Params:" + mAddLoginInfoReq.toString());

		HttpRequest.post(url, mParams, new BaseHttpRequestCallback<String>() {

			@Override
			protected void onSuccess(String o) {
				super.onSuccess(o);
				try {
					String result = CodecUtils.base64Decode(o);
					Log.i(TAG, "----->Result:" + result);
				}catch (Exception e){
					e.printStackTrace();
				}
			}

			@Override
			public void onResponse(String response, Headers headers) {
			}

			@Override
			public void onFailure(int errorCode, String msg) {
				super.onFailure(errorCode, msg);
				Log.i(TAG, "----->Result:arg1:" + errorCode + ",arg2:" + msg);
				if (errorCode == UIHelper.HTTP_CODE_TIMEOUT){
					mMessage.what = UIHelper.HTTP_CODE_TIMEOUT;
				}else {
					mMessage.what = 3;
				}
				send();
			}
		});
	}

	/* (non-Javadoc)
	 * @see com.jason.fingerprint.logic.Logic#destory()
	 */
	@Override
	public void destory() {
		// TODO Auto-generated method stub
		
	}

}
